import {NgModule} from '@angular/core';
import {GnSiteInfo} from './SiteInfo';
import {GnLoginUser} from './LoginUser';
import {GnRouteGuard} from './RouteGuard';
import {GnHttpInterceptorConfig} from './HttpInterceptorConfig';
import {HTTP_INTERCEPTORS, HttpClient, HttpClientModule} from '@angular/common/http';
import {GnOAuthHttpInterceptor} from './OAuthHttpInterceptor';
import zh from '@angular/common/locales/zh';
import {registerLocaleData} from '@angular/common';
import {GnMessageService} from './MessageService';
import {GnIconService} from './IconService';

registerLocaleData(zh);

@NgModule({
  declarations: [],
  exports: [],
  imports: [
    HttpClientModule,
  ],
  providers: [
    GnSiteInfo,
    GnLoginUser,
    GnRouteGuard,
    GnHttpInterceptorConfig,
    {provide: HTTP_INTERCEPTORS, useClass: GnOAuthHttpInterceptor, multi: true}
  ]
})
export class GnAppModule {

  public static CONFIG_PATH = 'assets/config/siteInfo.json';

  constructor(httpClient: HttpClient,
              siteInfo: GnSiteInfo,
              iconService: GnIconService,
              messageService: GnMessageService) {
    httpClient.get(GnAppModule.CONFIG_PATH)
      .subscribe((value: GnSiteInfo) => {
          siteInfo.title = value.title;
          siteInfo.loginUrl = value.loginUrl;
          siteInfo.logoutUrl = value.logoutUrl;
          siteInfo.iconFontUrl = value.iconFontUrl;
          siteInfo.showBreadcrumb = value.showBreadcrumb;
          siteInfo.siderMenuTheme = value.siderMenuTheme || 'dark';
          siteInfo.siderMenu = value.siderMenu;
          siteInfo.siteAuthorities = value.siteAuthorities;

          siteInfo.authSiderMenu = siteInfo.getAuthorityMenu(siteInfo.siderMenu, '');

          iconService.load(siteInfo.iconFontUrl);
        },
        error => {
          messageService.error('获取系统信息错误');
          throw error;
        });
  }

}
